from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .database import engine, Base
# 导入新的chat路由
from .routers import health, prompts, auth, tags, user, chat, template

# 创建所有数据库表
Base.metadata.create_all(bind=engine)

# 初始化FastAPI应用，显式配置文档
app = FastAPI(
    title='PromptMinder API',
    description='Backend API for PromptMinder application',
    version='1.0.0',
    docs_url='/docs',  # 显式指定Swagger UI路径
    redoc_url='/redoc',  # 显式指定ReDoc路径
    openapi_url='/openapi.json'  # 显式指定OpenAPI规范路径
)

# 配置CORS
origins = [
    'http://localhost:3000',  # 前端应用地址
    'http://127.0.0.1:3000',
    'http://localhost:3001',  # 后端应用地址
    'http://127.0.0.1:3001',
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

# 注册路由
app.include_router(health.router)
app.include_router(prompts.router)
app.include_router(auth.router)
app.include_router(tags.router)
app.include_router(user.router)
app.include_router(chat.router)
app.include_router(template.router)  # 移除prefix参数，使用router自身的prefix设置

# 根路由
@app.get('/')
def read_root():
    return {'message': 'Welcome to PromptMinder API'}